package com.ggh.jinjilive.ui.mall.goods.affirm;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityGoodsAffirmOrderBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.CommonMobile;
import com.ggh.jinjilive.mobile.Pay.WeChatPayMobile;
import com.ggh.jinjilive.mobile.mall.goods.GoodsDetailsMobile;
import com.ggh.jinjilive.mobile.mall.goods.GoodsShopMobile;
import com.ggh.jinjilive.mobile.mall.goods.GoodsShopSPMobile;
import com.ggh.jinjilive.mobile.mall.goods.GoodsSpecificationMobile;
import com.ggh.jinjilive.mobile.mall.goods.thegoods.TheGoodsAddressMobile;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.pay.PayUtils;
import com.gyf.immersionbar.ImmersionBar;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Route(path = ARouterUtils.GoodsAffirmOrderActivity)
public class GoodsAffirmOrderActivity extends BaseActivity<ActivityGoodsAffirmOrderBinding> implements View.OnClickListener{
    //VM
    private AffirmOrderViewModel affirmOrderViewModel;
    //详情信息
    private GoodsDetailsMobile goodsDetailsMobile;
    //当前的规格
    private GoodsSpecificationMobile goodsSpecificationMobile;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        affirmOrderViewModel = new ViewModelProvider(this,viewModelFactory).get(AffirmOrderViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_goods_affirm_order;

    }

    @Override
    public void initParam() {
        super.initParam();
        goodsDetailsMobile = (GoodsDetailsMobile) getIntent().getSerializableExtra("goodsDetailsMobile");
        goodsSpecificationMobile = (GoodsSpecificationMobile) getIntent().getSerializableExtra("GoodsSpecificationMobile");
    }

    @Override
    public void initView() {
        super.initView();
        setWhiteActionBarStyle("确认订单",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);

    }

    @Override
    public void initData() {
        super.initData();
        //获取收货地址
        affirmOrderViewModel.receiveAddressList();
        //详情
        if(null != goodsDetailsMobile){
            //店铺图片
            Glide.with(binding.storeImg).load(goodsDetailsMobile.getStore_img()).into(binding.storeImg);
            //店铺名称
            binding.storeName.setText(goodsDetailsMobile.getStore_name());
            //商品图片
            Glide.with(binding.riv).load(goodsDetailsMobile.getImg()).into(binding.riv);
            //商品内容
            binding.tvDescribes.setText(goodsDetailsMobile.getDescribes());
            //规格信息
            if(null != goodsSpecificationMobile){
                //价格
                binding.tvFee.setText("¥"+goodsSpecificationMobile.getFee());
                //数量
                binding.tvMagnitude.setText("x" + goodsSpecificationMobile.getGoodsQuantity());
                binding.etMagnitude.setText(goodsSpecificationMobile.getGoodsQuantity() +"");
                binding.tvCommon.setText("共" + goodsSpecificationMobile.getGoodsQuantity() + "件");
                //价格
                double price = (goodsSpecificationMobile.getGoodsQuantity() * goodsSpecificationMobile.getFee());
                binding.tvPrice.setText(price +"");
                //规格
                binding.tvSpecification.setText("选择的规格: " + goodsSpecificationMobile.getName());
            }
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //没地址
        binding.tvNoAddress.setOnClickListener(this);
        //修改
        binding.ButtonModification.setOnClickListener(this);
        //减
        binding.imgSubtract.setOnClickListener(this);
        //加
        binding.imgAdd.setOnClickListener(this);
        //立即购买
        binding.ButtonImmediatelyBuy.setOnClickListener(this);
        //获取收货地址
        affirmOrderViewModel.TheGoodsAddressMobileList.observe(this, new Observer<List<TheGoodsAddressMobile>>() {
            @Override
            public void onChanged(List<TheGoodsAddressMobile> theGoodsAddressMobiles) {
                if(null != theGoodsAddressMobiles && theGoodsAddressMobiles.size() > 0){
                    //没地址
                    binding.tvNoAddress.setVisibility(View.GONE);
                    //有地址
                    binding.llHaveAddress.setVisibility(View.VISIBLE);
                    for (TheGoodsAddressMobile theGoodsAddressMobile : theGoodsAddressMobiles){
                        if(theGoodsAddressMobile.getIs_default() == 1){
                            //当前选择的地址
                            affirmOrderViewModel.mTheGoodsAddressMobile.set(theGoodsAddressMobile);
                            //姓名手机号
                            binding.tvNameNumber.setText(theGoodsAddressMobile.getName() +"   " + theGoodsAddressMobile.getMobile());
                            //地址
                            binding.tvAddress.setText(theGoodsAddressMobile.getContent()+ "  " + theGoodsAddressMobile.getS_content());
                        }
                    }
                    if(null != affirmOrderViewModel.mTheGoodsAddressMobile.get()){
                        //没地址
                        binding.tvNoAddress.setVisibility(View.GONE);
                        //有地址
                        binding.llHaveAddress.setVisibility(View.VISIBLE);
                    }else {
                        //没地址
                        binding.tvNoAddress.setVisibility(View.VISIBLE);
                        //有地址
                        binding.llHaveAddress.setVisibility(View.GONE);
                    }

                }else {
                    //没地址
                    binding.tvNoAddress.setVisibility(View.VISIBLE);
                    //有地址
                    binding.llHaveAddress.setVisibility(View.GONE);
                }
            }
        });
        //添加订单
        affirmOrderViewModel.mCommonMobile.observe(this, new Observer<CommonMobile>() {
            @Override
            public void onChanged(CommonMobile commonMobile) {
                Map<String, Object> map = new HashMap<>();
                map.put("order_sn",commonMobile.getOrder_sn());
                map.put("pay_type",WeChatAlipay);
                if(WeChatAlipay == 0){//微信
                    affirmOrderViewModel.weChatpayOrder(map);
                }else if(WeChatAlipay == 1){//支付宝
                    affirmOrderViewModel.alipayPayOrder(map);
                }
            }
        });
        //微信付款信息
        affirmOrderViewModel.mWeChatPayMobile.observe(this, new Observer<WeChatPayMobile>() {
            @Override
            public void onChanged(WeChatPayMobile weChatPayMobile) {
                //调起微信
                PayUtils.getInstance(mContext).WeChatPay(weChatPayMobile);
            }
        });
        //支付宝付款信息
        affirmOrderViewModel.mAlipayString.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String AlipayPay) {
                //调起支付宝
                PayUtils.getInstance(mContext).AlipayPay(AlipayPay);
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.Button_modification://修改
                ARouter.getInstance().build(ARouterUtils.TheGoodsActivity)
                        .withSerializable("mTheGoodsAddressMobile",affirmOrderViewModel.mTheGoodsAddressMobile.get())
                        .withString("distinguish","确认订单")
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation(GoodsAffirmOrderActivity.this,105);
                break;
            case R.id.tv_no_address://没地址
                ARouter.getInstance().build(ARouterUtils.TheGoodsActivity)
                        .withString("distinguish","确认订单")
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation(GoodsAffirmOrderActivity.this,105);
                break;
            case R.id.img_subtract://减
                if(null != goodsSpecificationMobile){
                    if(goodsSpecificationMobile.getGoodsQuantity() == 1){
                        ToastUtils.make().show("数量低于范围~");
                        return;
                    }
                    goodsSpecificationMobile.setGoodsQuantity(goodsSpecificationMobile.getGoodsQuantity() - 1);
                    //数量
                    binding.tvMagnitude.setText("x" + goodsSpecificationMobile.getGoodsQuantity());
                    binding.etMagnitude.setText(goodsSpecificationMobile.getGoodsQuantity() +"");
                    binding.tvCommon.setText("共" + goodsSpecificationMobile.getGoodsQuantity() + "件");
                    //价格
                    double price = (goodsSpecificationMobile.getGoodsQuantity() * goodsSpecificationMobile.getFee());
                    binding.tvPrice.setText("¥" + decimalFormat.format(price));
                }
                break;
            case R.id.img_add://加
                if(null != goodsSpecificationMobile){
                    if(goodsSpecificationMobile.getGoodsQuantity() < goodsSpecificationMobile.getStock()){
                        goodsSpecificationMobile.setGoodsQuantity(goodsSpecificationMobile.getGoodsQuantity() + 1);
                        //数量
                        binding.tvMagnitude.setText("x" + goodsSpecificationMobile.getGoodsQuantity());
                        binding.etMagnitude.setText(goodsSpecificationMobile.getGoodsQuantity() +"");
                        binding.tvCommon.setText("共" + goodsSpecificationMobile.getGoodsQuantity() + "件");
                        //价格
                        double price = (goodsSpecificationMobile.getGoodsQuantity() * goodsSpecificationMobile.getFee());
                        binding.tvPrice.setText("¥" + decimalFormat.format(price));

                    }else {
                        ToastUtils.make().show("大于库存~");
                    }
                }
                break;
            case R.id.Button_immediately_buy://立即购买
                if(null != payChooseDialog && !payChooseDialog.isShowing()){
                    payChooseDialog.show();
                    //价格
                    double price = (goodsSpecificationMobile.getGoodsQuantity() * goodsSpecificationMobile.getFee());
                    payChooseDialog.setPayMoneyContet(price +"");
                    payChooseDialog.setAddItemOnClickListener(new AddItemOnClickListener() {
                        @Override
                        public void onSkipClicked(int position, Object mObject) {
                            WeChatAlipay = position;
                            Map<String, Object> map = new HashMap<>();
                            map.put("type",0);
                            map.put("cid",0);
                            map.put("price", price);
                            map.put("pay_type",position);
                            map.put("remark",binding.etNote.getText().toString());
                            map.put("send_id",affirmOrderViewModel.mTheGoodsAddressMobile.get().getId());
                            //规格
                            List<GoodsShopMobile> goodsShopMobileList = new ArrayList<>();
                            GoodsShopMobile goodsShopMobile = new GoodsShopMobile();
                            //商品的ID
                            goodsShopMobile.setPrice(price +"");
                            //店铺ID
                            goodsShopMobile.setSid(goodsDetailsMobile.getSid() +"");
                            List<GoodsShopSPMobile> goodsShopSPMobileList = new ArrayList<>();
                            GoodsShopSPMobile goodsShopSPMobile = new GoodsShopSPMobile();
                            //数量
                            goodsShopSPMobile.setNum(goodsSpecificationMobile.getGoodsQuantity());
                            //价格
                            goodsShopSPMobile.setPrice(goodsSpecificationMobile.getFee());
                            //商品ID
                            goodsShopSPMobile.setProduct_id(goodsDetailsMobile.getId());
                            //规格ID
                            goodsShopSPMobile.setProduct_spec_id(goodsSpecificationMobile.getId());
                            //插入数据
                            goodsShopSPMobileList.add(goodsShopSPMobile);
                            //商品数据
                            goodsShopMobile.setProduct(goodsShopSPMobileList);
                            goodsShopMobileList.add(goodsShopMobile);
                            map.put("shop",goodsShopMobileList);
                            affirmOrderViewModel.addOrder(map);
                            payChooseDialog.dismiss();
                        }
                    });
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case 105:
                TheGoodsAddressMobile theGoodsAddressMobile = (TheGoodsAddressMobile) data.getSerializableExtra("theGoodsAddressMobile");
                if(null != theGoodsAddressMobile){
                    //没地址
                    binding.tvNoAddress.setVisibility(View.GONE);
                    //有地址
                    binding.llHaveAddress.setVisibility(View.VISIBLE);
                    //当前选择的地址
                    affirmOrderViewModel.mTheGoodsAddressMobile.set(theGoodsAddressMobile);
                    //姓名手机号
                    binding.tvNameNumber.setText(theGoodsAddressMobile.getName() +"   " + theGoodsAddressMobile.getMobile());
                    //地址
                    binding.tvAddress.setText(theGoodsAddressMobile.getContent()+ "  " + theGoodsAddressMobile.getS_content());

                }
                break;
        }
    }
}
